/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/

#include "pch.h"
#include <db/db_index_instance.h>
#include <db/db_select_criteria.h>

namespace test_wrappers { namespace storage {

index_instance::index_instance(
    const index_definition& indexDefinition,
    const std::shared_ptr<tuple_instance>& tupleInstance) :
        _tupleInstance(tupleInstance),
        _userIndexId(indexDefinition.userIndexId()),
        _name(indexDefinition.name()),
        _primary(indexDefinition.primary()),
        _unique(indexDefinition.unique())
{
    for (auto &indexDefinition : indexDefinition.indexFields())
    {
        _indexFields.push_back(index_field_instance(indexDefinition));
    }
}

std::shared_ptr<index_instance> index_instance::create(
    const index_definition& indexDefinition,
    const std::shared_ptr<tuple_instance>& tupleInstance)
{
    return std::make_shared<index_instance>(indexDefinition, tupleInstance);
}

field_id index_instance::userIndexId() const
{
    return _userIndexId;
}

std::wstring index_instance::name() const
{
    return _name;
}

bool index_instance::primary() const
{
    return _primary;
}

bool index_instance::unique() const
{
    return _unique;
}

const std::vector<index_field_instance>& index_instance::indexFields() const
{
    return _indexFields;
}

tuple_values index_instance::createValues()
{
    std::shared_ptr<std::vector<std::shared_ptr<field_value>>> fieldValues =
        std::make_shared<std::vector<std::shared_ptr<field_value>>>();

    for (auto &indexField : _indexFields)
    {
        std::shared_ptr<field_instance> fieldInstance = _tupleInstance->fieldByUserFieldId(indexField.userFieldId());

        std::shared_ptr<field_value> fieldValue;
        switch (fieldInstance->type())
        {
        case type_unknown:
            break;
        case type_int16:
            fieldValue = createValue<unsigned short>(fieldInstance->fieldInstanceId(), 0);
            break;
        case type_int32:
            fieldValue = createValue<unsigned int>(fieldInstance->fieldInstanceId(), 0);
            break;
        case type_int64:
            fieldValue = createValue<unsigned __int64>(fieldInstance->fieldInstanceId(), 0);
            break;
        case type_float32:
            fieldValue = createValue<float>(fieldInstance->fieldInstanceId(), 0.0f);
            break;
        //case type_string:
        //    fieldValue = (std::dynamic_pointer_cast<field_definition_t<std::wstring>>(field))->createValue(L"");
        //    break;
        case type_string_long:
            fieldValue = createValue<std::wstring>(fieldInstance->fieldInstanceId(), L"");
            break;
        case type_binaryBlob:
            fieldValue = createValue<binary_blob>(fieldInstance->fieldInstanceId(), 0);
            break;
        default:
            _ASSERTE(false);
        }
        fieldValues->push_back(fieldValue);
    }

    return tuple_values(_tupleInstance, fieldValues);
}

const select_criteria index_instance::createSelectCriteria(
    const comparison_operator& comparisonOperator
    )
{
    return select_criteria(shared_from_this(), createValues(), comparisonOperator);
}


}}// namespace test_wrappers::storage
